#ifndef ZKHY_TOOLS_COMMON_H_
#define ZKHY_TOOLS_COMMON_H_
#include<tool/libcell_tools_kd_node.h>
#define quote lib_cell_quote
#define _ReadOff_   LibCell_ReadOff_
#define _ReadCell_  LibCell_ReadCell_
#define _WriteCell_ LibCell_WriteCell_
#define _ReadArray_ LibCell_ReadArray_
#ifdef __cplusplus
extern "C" {
#endif

static void points_obj2off(char const * filename)
{
      int filenamesize=strlen(filename);
    char *outfilename=(char*)malloc(sizeof(char)*(filenamesize+2));
    strcpy(outfilename,filename);
    strcpy(&(outfilename[filenamesize-3]),"off");
    printf("%s\n",outfilename);
#ifdef _WIN32
    FILE *infile,*outfile;
    fopen_s(&infile,filename,"r");
    fopen_s(&outfile,outfilename,"w");

#else
    FILE * infile=fopen(filename,"r");
    FILE *outfile=fopen(outfilename,"w");
#endif
  
    if(!infile)
    {
        printf("can't open this file %s\n",filename);
        return;
    }
    if(!outfile)
    {
        printf("can't open this file %s\n",filename);
        return;
    }
    fseek(infile,0,SEEK_SET);
    fseek(outfile,0,SEEK_SET);
    fprintf(outfile, "%s\n","OFF" );
    fprintf(outfile, "                           \n" );
    int v_rows=0;
 //   double p1=0,p2=0,p3=0;

    char str[200];
     
    while(!feof(infile))
    {
        fgets(str,200,infile);
        if(str[0]=='#')
        {
            continue;
        } 
        else if(str[0]=='v'&&str[1]==' ')
        {

            fprintf(outfile, "%s", &str[2] ); 
            v_rows++;
            //sscanf(&str[2],"%lf %")
            //fprintf(outfile, "%d\n", );
            //printf("%s\n",str );
        }
    
        
    }
    fclose(infile);
    fclose(outfile); 

   
     printf("v_rows%d\n",v_rows );
#ifdef _WIN32
    fopen_s(&outfile,outfilename,"r+");   
#else
    outfile=fopen(outfilename,"r+");
#endif
    fseek(outfile,0,SEEK_SET);
    //fgets(str,200,outfile);
    fprintf(outfile, "OFF\n%d %d %d",v_rows,0,0 );

    fclose(outfile); 

    free(outfilename); 
    
}
static void _Read_(Mesh*mesh,char const * filename)
{
    int i=0;
    while(filename[i]!='\0')
    {
        i++;
    }
    i--;
    while(filename[i]!='.'&&filename[i]!='\0'&&i>0)
    {

        i--;
    }
   // printf("%c\n",filename[i] );
    if(filename[i]=='\0'||filename[i+1]=='\0'||filename[i+2]=='\0')
    {return;} 
    char fn[200];
    strcpy(fn,filename);
    if(filename[i+1]=='o'&&filename[i+2]=='b'&&filename[i+3]=='j') 
    {
        fn[i+1]='o';fn[i+2]='f';fn[i+3]='f';
        points_obj2off(filename);
        _ReadOff_(mesh,fn,3); 
    }
    else if(filename[i+1]=='o'&&filename[i+2]=='f'&&filename[i+3]=='f')
    {
       // printf("read off\n"); 
        _ReadOff_(mesh,filename,3);
    }
    else if(filename[i+1]=='c'&&filename[i+2]=='e'&&filename[i+3]=='l'&&filename[i+4]=='l')
    {
       // printf("here\n");
        _ReadCell_(mesh,filename);
    }
}
#ifdef __cplusplus
}
#endif

#undef quote 
#undef _ReadOff_ 
#undef _ReadCell_ 
#undef _WriteCell_ 
#undef _ReadArray_ 
#endif
